<template>
	<view class="container">
		<view class="logo">
			<image class="img" src="/static/assets/login-logo.png" mode="aspectFit" />
		</view>
		<view class="title">注册账户</view>
		<view class="input-box">
			<input type="text" v-model="user.nickName" placeholder="用户名" />
			<input type="email" v-model="user.email" placeholder="请使用邮箱注册" />
			<input type="password" v-model="user.password" placeholder="密码" />
			<input class="input" v-model="user.password2" type="password" placeholder="确认密码" />
		</view>
		<button class="register-button" @click="registerFn">立即注册</button>
		<navigator class="login-link" url="/pages/login/login">已有账号？登录</navigator>
	</view>
</template>

<script>
	import api from '../../api/index';
	export default {
		data() {
			return {
				user: {
					nickName: "",
					email: "",
					password: "",
					password2: "",
				}
			};
		},
		methods: {
			async registerFn() {
				// 注册逻辑
				let {
					nickName,
					email,
					password,
					password2
				} = this.user;
				// 邮箱格式的正则表达式
				const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
				if (nickName && email && password && password2) {
					// 验证邮箱格式
					if (!emailPattern.test(email)) {
						uni.showToast({
							title: "请输入有效的邮箱地址",
							icon: 'none'
						});
						return; // 终止后续执行
					}
					if (password == password2) {
						api.register({
								nickName,
								email,
								password
							})
							.then(
								content => {
									console.log(content);
									let {
										code,
										msg
									} = content;
									if (code == 200) {
										// console.log("登录成功");
										uni.showToast({
											title: msg,
											icon: 'success'
										})
										setTimeout(() => {
											uni.navigateTo({
												url: '/pages/login/login'
											})
										}, 300)
									} else {
										console.log("登录失败");
										uni.showToast({
											title: msg,
											icon: 'error'
										})
									}
								})
					} else {
						uni.showToast({
							title: "两次输入的密码不一致",
							icon: 'none'
						})
					}
				} else {
					uni.showToast({
						title: "输入框不能为空",
						icon: 'none'
					})
				}

			}
		}
	};
</script>

<style lang="scss" scoped>
	@import "../../uni.scss";

	.container {
		display: flex;
		flex-direction: column;
		// row-gap: 15rpx;
		justify-content: center;
		align-items: center;
		height: 100vh;
		background-color: #f8f8f8;
	}

	.logo {
		margin-bottom: 10rpx;

		.img {
			width: 100px;
			height: 100px;
		}
	}

	.title {
		font-size: 24px;
		color: #ff6a6a;
		margin-bottom: 20px;
	}

	.input-box {
		width: 80%;
		background-color: #ffffff;
		padding: 20px;
		border-radius: 10px;
		box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
		margin-bottom: 20px;
	}

	.input-box input {
		width: 95%;
		padding: 10px;
		margin: 5px 0;
		border-bottom: 1px solid #ccc;
		border-radius: 5px;
	}

	.register-button {
		width: 80%;
		height: 100rpx;
		background: $bgBottom;
		color: #ffffff;
		border: none;
		border-radius: 25px;
		font-size: 18px;
		text-align: center;
		box-shadow: 0 5px 10px rgba(0, 0, 0, 0.1);
		margin-top: 20rpx;
	}

	.login-link {
		margin-top: 15px;
		color: #ff6e67;
		text-decoration: none;
		font-size: 14px;
	}

	.login-link:hover {
		text-decoration: underline;
	}
</style>